﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySafeLib;

namespace CADUI
{
    public partial class ValueSlider : UserControl
    {
        public delegate void DelOnValueChanged(ValueChangedArgs valueChanged);

        [Browsable( true ), Category( "Slider events" )]
        public event DelOnValueChanged OnValueChanged = null;

        [Browsable( true ), Category( "Slider Values" )]
        public int Minimum
        {
            get
            {
                return this.slider.Minimum;
            }
            set
            {
                this.slider.Minimum = value;
            }
        }

        [Browsable( true ), Category( "Slider Values" )]
        public int Maximum
        {
            get
            {
                return this.slider.Maximum;
            }
            set
            {
                this.slider.Maximum = value;
            }
        }

        [Browsable( true ), Category( "Slider Values" )]
        public double Value
        {
            get
            {
                try
                {
                    return double.Parse( this.valueTextBox.Text );
                }
                catch (ArgumentNullException)
                {
                }
                catch (FormatException)
                {
                }
                catch (OverflowException)
                {
                }
                return 0.0;
            }
            set
            {
                this.valueTextBox.Text = value.ToString();
            }
        }

        [Browsable( true ), Category( "Slider Values" )]
        public int SmallChange
        {
            get
            {
                return this.slider.SmallChange;
            }
            set
            {
                this.slider.SmallChange = value;
            }
        }

        [Browsable( true ), Category( "Slider Values" )]
        public int LargeChange
        {
            get
            {
                return this.slider.LargeChange;
            }
            set
            {
                this.slider.LargeChange = value;
            }
        }

        public ValueSlider()
        {
            InitializeComponent();
        }

        private void slider_ValueChanged(object sender, EventArgs e)
        {
            this.valueTextBox.Text = this.slider.Value.ToString();
            if (this.OnValueChanged != null)
                this.OnValueChanged( new ValueChangedArgs( this.slider.Value ) );
        }

        private void valueTextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.valueTextBox.Text == String.Empty || this.OnValueChanged == null)
                return;
            int value = 0;
            SafeStringParser<int> parser = new SafeStringParser<int>();
            parser.Parse( this.valueTextBox.Text, ref value );
            this.OnValueChanged( new ValueChangedArgs( value ) );
        }
    }
}
